package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsBrand;
import com.qf.service.DtsBrandService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * @author Cc
 * @version 1.0
 */

@CrossOrigin("*")
@RestController
@RequestMapping("/admin/brand")
public class AdminBrandController {

    @Autowired
    private DtsBrandService dtsBrandService;

    /**
     * 接口地址 : http://localhost:8083/admin/brand/list?page=1&limit=20&sort=add_time&order=desc
     */
    @RequiresPermissions("admin:brand:list")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商"}, button = "品牌制造商列表")
    @GetMapping("/list")
    public  Object list(@RequestParam(defaultValue = "1") Integer page,
                        @RequestParam(defaultValue = "10") Integer limit,
                        @RequestParam(defaultValue = "add_time") String sort,
                        @RequestParam(defaultValue = "desc") String order){

        IPage<DtsBrand> iPage = dtsBrandService.querySelective( page, limit, sort, order);

        HashMap map= new HashMap();
        map.put("total", iPage.getTotal());
        map.put("items", iPage.getRecords());
        return ResponseUtil.ok(map);
    }

    /**
     * 接口地址 :  http://localhost:8083/admin/brand/catAndAdmin
     */
    @RequiresPermissions("admin:brand:catAndAdmin")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商"}, button = "品牌和管理员列表")
    @GetMapping("/catAndAdmin")
    public Object catAndAdmin(){
        List<CatVo> idAndUsername = dtsBrandService.findIdAndUsername();
        List<CatVo> findCategory = dtsBrandService.findcategory();
        List<CatVo> findChilder = dtsBrandService.findchilder();
        CatVo catVo = new CatVo();
        catVo.setChildren(findChilder);
        HashMap map = new HashMap();
        map.put("adminList",idAndUsername);
        for(CatVo category:findCategory){
            Integer value = category.getValue();
            String label = category.getLabel();
            catVo.setValue(value);
            catVo.setLabel(label);
        }
        map.put("categoryList",findCategory);
        return ResponseUtil.ok(map);
    }

    /**
     * 接口地址 : http://localhost:8083/admin/brand/create
     */
    @RequiresPermissions("admin:brand:create")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsBrand brand){
            dtsBrandService.save(brand);
            return ResponseUtil.ok();
    }

    /**
     * 接口地址 : http://localhost:8083/admin/brand/update
     */
    @RequiresPermissions("admin:brand:update")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商"}, button = "修改")
    @PostMapping("/update")
    public Object update(@RequestBody DtsBrand brand){
        dtsBrandService.update(brand);
        return ResponseUtil.ok();
    }

    /**
     * 接口地址 : http://localhost:8083/admin/brand/delete
     */
    @RequiresPermissions("admin:brand:delete")
    @RequiresPermissionsDesc(menu = {"商场管理", "品牌制造商"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsBrand brand){
        Integer id = brand.getId();
        dtsBrandService.delete(id);
        return ResponseUtil.ok();
    }
}
